﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
using DalGen.Entity;
using DalGen.Util;
namespace DalGen.ConnForm
{
	public partial class MySQLForm : Form
	{
		public MySQLForm()
		{
			InitializeComponent();
		}

        private DataTable TestConn()
        {
            string uid = txtuid.Text;
            string pwd = txtpwd.Text;
            string port = txtport.Text;
            string server = txtserver.Text;
            string connStr = "Server=" + server + ";Port=" + port + ";Password=" + pwd + ";User ID=" + uid + ";";

            DataTable dtTables = new DataTable();

            using (MySqlConnection _conn = new MySqlConnection(connStr))
            {
                if (_conn.State == ConnectionState.Closed)
                    _conn.Open();

                
                MySqlDataAdapter da = new MySqlDataAdapter();


                da.SelectCommand = new MySqlCommand("show databases", _conn);
                da.Fill(dtTables);


                if (_conn.State == ConnectionState.Open)
                {
                    _conn.Close();
                }

            }


            return dtTables;

        }

        private void btnconn_Click(object sender, EventArgs e)
        {
            DataTable dtTables = new DataTable();
            try
            {
                dtTables=TestConn();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
            }

            comboBox1.DisplayMember = "Database";
            comboBox1.ValueMember = "Database";
            comboBox1.DataSource = dtTables;

          
            

        }

        private void button2_Click(object sender, EventArgs e)
        {
            BuilderInfo entity = new BuilderInfo();
            entity.Server = txtserver.Text+":"+txtport.Text;
            entity.UID = txtuid.Text;            
            entity.DataBaseName = comboBox1.SelectedValue.ToString();            
            entity.ConnectionString = "Server=" + txtserver.Text + ";Port=" + txtport.Text + ";database=" + comboBox1.SelectedValue.ToString() + ";Password=" + txtpwd.Text + ";User ID=" + txtuid.Text + ";";
            entity.DataBaseType = "MySql";
            entity.DataBaseVer = "5.15";
            entity.Guid = Guid.NewGuid().ToString();
            entity.Alias = txtAlias.Text;

            //查询版本号


            using (MySqlConnection _conn = new MySqlConnection(entity.ConnectionString))
            {
                if (_conn.State == ConnectionState.Closed)
                    _conn.Open();

                MySqlCommand cmd = new MySqlCommand("select version()", _conn);

                entity.DataBaseVer=cmd.ExecuteScalar().ToString();

                if (_conn.State == ConnectionState.Open)
                {
                    _conn.Close();
                }

            }



            XmlHelper.AddDalInfo(entity);

            ManageForm form = new ManageForm();
            form.Show();
            this.Hide(); 

        }

		private void MysqlForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			ManageForm form = new ManageForm();
			form.Show();
		}

        private void button3_Click(object sender, EventArgs e)
        {
            this.Close();
            ManageForm form = new ManageForm();
            form.Show();
        }
	}
}
